//app.js
const http = require("./utils/http.js");
App({
    observe: function (page) {
        // 观察的数据对象
        let observeObj = page.data;
        let observePros = Object.keys(page.watch);
        observePros.forEach(pro => {
            let _value = page.data[pro];
            console.log(_value);
            Object.defineProperty(observeObj, pro, {
                set: value => {
                    if (value !== _value) {
                        page.watch[pro].call(page, value, _value);
                        _value = value;
                    }
                },
                get: () => _value
            });
        });
    },

    onLaunch: function () {
        this.login();
        this.globalData = {
            toCartWay: "",
            Api: {
                category: {
                    list: fid => http({
                        url: "/category/list/" + fid
                    })
                },
                product: {
                    list: (data) => http({
                        url: "/product/list",
                        data,
                        method: "post"
                    }, false)
                },
                detail: {
                    list: (pid) => http({
                        url: `/product/model/${pid}`
                    }),
                    addProduct: (data) => http({
                        url: "/cart/add_product",
                        data,
                        method: "post"
                    }),
                    getTotal: () => http({
                        url: "/cart/total"
                    })
                },
                Login: {
                    login: data => http({
                        url: `/user/login_pwd`,
                        method: "post",
                        data
                    })
                },
                user: {
                    login: code => http({
                        url: "/user/login/" + code
                    })
                },
                cart: {
                    getList() {
                        return http({
                            url: "/cart/list",
                        });
                    },
                    decrease(id) {
                        return http({
                            url: `/cart/deincrease/${id}`
                        }, false);
                    },
                    increase(id) {
                        return http({
                            url: `/cart/increase/${id}`
                        }, false);
                    },
                    remove: data => http({
                        url: "/cart/batch_remove",
                        method: "post",
                        data
                    }),
                    list_ids: data => http({
                        url: '/cart/list_ids',
                        method: "post",
                        data
                    }, false)
                },
                address: {
                    getList() {
                        return http({
                            url: "/address/list"
                        })
                    },
                    remove(id) {
                        return http({
                            url: `/address/remove/${id}`
                        })
                    },
                    set_default: (id) => http({
                        url: `/address/set_default/${id}`
                    }),
                    addAddress(data) {
                        return http({
                            url: "/address/add",
                            method: "post",
                            data
                        })
                    },
                    updateAddress(data) {
                        return http({
                            url: "/address/update",
                            method: "post",
                            data
                        })
                    },
                    // 获取某个用户的默认地址
                    get_default() {
                        return http({
                            url: "/address/get_default"
                        },false)
                    }
                },
                order : {
                    addOrderList(data){
                        return http({
                            url : "/order/add",
                            method : "post",
                            data
                        });
                    }
                }
            },

        };
    },
    login: function () {
        wx.login({
            timeout: 10000,
            success: async (result) => {
                console.log(result.code);
                const token = await this.globalData.Api.user.login(result.code);
                wx.setStorageSync('token', token)
            },
            fail: () => {},
            complete: () => {}
        });
    }
})